Skip to content

Fix unit-result special cases in real pow - #1460

Open
umrndem wants to merge 4 commits into
xtensor-stack:masterfrom
umrndem:fix/pow-unit-special-cases
Open

umrndem wants to merge 4 commits into
xtensor-stack:masterfrom
umrndem:fix/pow-unit-special-cases

Conversation

@umrndem

@umrndem umrndem commented Nov 20, 2026 •

Copy link
Copy Markdown

Fix real pow to return 1 for a zero exponent, a base of 1, or a base of -1 with an infinite exponent. Handle those lanes before multiplying the exponent by the logarithm.

Added four regression checks to the existing power test for float and double. The real and complex power tests passed on SSE2 and AVX2 with FMA.

Fixes #1458.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

sorry, there have been too many PRs lately, so I’m scaling back how many I review carefully. PRs that take more time to review compared of fixing the problem myself will not be reviewed.

I wouldn’t review this PR until the diff is much smaller than 72 lines. We’re fixing a single special case, and I don’t see a reason for such a large diff.

The test should be minimal too. AI tends to overtest by checking the same thing repeatedly. Also it tends to change more things than needed.

If you’re using AI, try Ponytail, ste and cpp skills, or Unslop to reduce boilerplate.

@umrndem

umrndem commented Nov 20, 2026

Copy link
Copy Markdown
Author

sorry, there have been too many PRs lately, so I’m scaling back how many I review carefully. PRs that take more time to review compared of fixing the problem myself will not be reviewed.

I wouldn’t review this PR until the diff is much smaller than 72 lines. We’re fixing a single special case, and I don’t see a reason for such a large diff.

The test should be minimal too. AI tends to overtest by checking the same thing repeatedly. Also it tends to change more things than needed.

If you’re using AI, try Ponytail, ste and cpp skills, or Unslop to reduce boilerplate.

Understood, I'll take another look and see if something can be done to reduce the diff.

@umrndem

umrndem commented Nov 20, 2026

Copy link
Copy Markdown
Author

thanks for the feedback. i got the diff down to 13 changed lines. the fix uses one mask to identify the cases where pow should return 1 and feeds those lanes 1^0 before the existing calculation. that avoids intermediate NaNs and keeps the rest of the calculation the same.

i cut the regression coverage down to four checks in the existing power test. the power tests passed on SSE2 and AVX2.

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This starts to make sense. Now we can try to integrate in the original algorithm by simplifying things.

const auto zero = batch_type(0.);
const auto one = batch_type(1.);
const auto unit_result = exponent == zero || base == one || (base == -one && isinf(exponent));
const auto self = select(unit_result, one, base);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep using base and exponent instead of self/other since you are touching this code. I think is more clear.

const auto self = select(unit_result, one, base);
const auto other = select(unit_result, zero, exponent);
auto negself = self < zero;
auto iszeropowpos = self == zero && other >= zero;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to simplify the control flow? We are now checking things equal zero here for example.

const auto other = select(unit_result, zero, exponent);
auto negself = self < zero;
auto iszeropowpos = self == zero && other >= zero;
auto adj_self = select(iszeropowpos, batch_type(1), abs(self));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is a one variable above we should keep using it.

@umrndem

umrndem commented Nov 21, 2026

Copy link
Copy Markdown
Author

thanks, i kept base and exponent throughout and reused one. the zero-base check now only looks at positive exponents, since the mask already handles zero exponents. i also reused the infinity check in both places. the power tests and all cases from #1458 passed on SSE2 and AVX2.

@DiamonDinoia

Copy link
Copy Markdown
Contributor

This make sense. Could you do a pass by adding comment such as x^0=1 and so on on the code to make it more readable. Just the math and the cases it handles?

Could you check the performance of the code? If we have regressions compared to the old code? If so, instead of select is it better to have a plain if with [[unlikely]] annotations that pushes the code out of the way? I feel like these corner cases are rare and should not impact the average use case.

@umrndem

umrndem commented Nov 21, 2026

Copy link
Copy Markdown
Author

thanks, i added the three identities above the mask. the two select calls now run only when at least one lane needs the fix, and mixed batches still handle those cases lane by lane.

i benchmarked ordinary float and double inputs against 4c44d61 on SSE2 and AVX2 with FMA, using GCC 15.2 at -O3 on one pinned i7-12800H core. the unconditional selects were about 8–17% slower than upstream. the plain guard brought that down to about 1–5% in paired runs. i tried [[unlikely]] too, and the plain guard was a little faster across all four configurations. the power tests and all the cases from #1458 passed on both targets.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pow returns incorrect values for 0 as exponent or 1 as base

2 participants